package com.mm.mindmap.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mm.common.config.FileProperties;
import com.mm.common.config.RedisProperties;
import com.mm.common.domain.po.Result;
import com.mm.common.enums.ResultCode;
import com.mm.common.pipeline.pipelineNodes.HandleNode;
import com.mm.common.utils.DataUtil;
import com.mm.common.utils.GZipUtil;
import com.mm.common.utils.UUIDUtil;
import com.mm.common.utils.SessionHolder;
import com.mm.mindmap.chache.MindMapCache;
import com.mm.mindmap.config.MindMapProperties;
import com.mm.mindmap.consistency.ConsistencyException;
import com.mm.mindmap.consistency.ConsistencyHandler;
import com.mm.mindmap.consistency.ConsistencyUtil;
import com.mm.mindmap.consistency.ConsistencyData;
import com.mm.mindmap.consistency.dataFunc.UpdateEs;
import com.mm.mindmap.consistency.dataFunc.UpdateMysql;
import com.mm.mindmap.consistency.dataFunc.UpdateRedis;
import com.mm.mindmap.consistency.strategy.scheme.DirectScheme;
import com.mm.mindmap.consistency.strategy.scheme.MQScheme;
import com.mm.mindmap.domain.dto.MindMapDTO;
import com.mm.mindmap.domain.dto.MindMapListDTO;
import com.mm.mindmap.domain.po.MindMap;
import com.mm.mindmap.handler.*;
import com.mm.mindmap.mapper.MindMapMapper;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;


/**
 * @BelongsProject: chatgpt-java-develop
 * @BelongsPackage: com.unfbx.service
 * @Author : 游在海里的鱼
 * @CreateTime : 2024-03-20  20:13
 * @Version : 1.0
 * @ClassName : TODO
 * @Description : TODO
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MindMapServiceImpl extends ServiceImpl<MindMapMapper, MindMap> implements IMindMapService {
    private final RedisProperties redisProperties;
    private final FileProperties fileProperties;
    private final MindMapProperties mmProperties;
    private final MindMapMapper mindMapMapper;
    private final EsServiceImpl esService;
    private final RedissonClient redissonClient;


    @Autowired
    @Lazy
    private  MindMapCache mindMapCache;




    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 新增导图
     * @Describe: TODO
     * @modifier: [public] [实例方法]
     **/

    public Result addMindMap(){
        //
        RLock lock = redissonClient.getLock(redisProperties.lock_mindmap_add + SessionHolder.getUser());
        MindMap cur = null;
        try {
            lock.lock();
            //检验是否有“未命名导图”没有处理
            MindMap mindMap = lambdaQuery()
                    .eq(MindMap::getTitle,mmProperties.default_mindmap_title)
                    .eq(MindMap::getUid, SessionHolder.getUser())
                    .one();
            if(mindMap != null){
                return Result.failed(ResultCode.FAILED_TITLE_DUPLICATIO);
            }
            //初始化导图
            mindMap = new MindMap();
            mindMap.setUid(SessionHolder.getUser());
            mindMap.setTitle(mmProperties.default_mindmap_title);
            initialize(mindMap);
            //保存导图
            if(!save(mindMap)){
                return Result.failed(ResultCode.FAILED);
            }
            //获取数据库里的导图
            cur = lambdaQuery()
                    .select(MindMap::getId)
                    .eq(MindMap::getTitle, mmProperties.default_mindmap_title)
                    .eq(MindMap::getUid, SessionHolder.getUser())
                    .one();
            if(cur == null){
                return Result.failed(ResultCode.FAILED);
            }
        } finally {
            lock.unlock();
        }
        //返回方案id
        return Result.success(cur.getId());
    }


    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 编辑导图
     * @Describe: TODO
     * @modifier: [public] [实例方法]
     **/
    public Result editMindMap(MindMap mindMap){
        //参数检验
        if(mindMap == null
                || DataUtil.isNaturalNumInvalid(mindMap.getId())
                || StrUtil.isBlank(mindMap.getTitle())
                || StrUtil.isBlank(mindMap.getNodeList())){
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        //解压缩
        mindMap.setNodeList(GZipUtil.deCompressStrByBase64(mindMap.getNodeList()));

//        if(true){
//            //导图为空，或uid对不上当前登录用户
//            MindMapDTO curMindMap = mindMapCache.getMindMap(mindMap.getId());
//            if( curMindMap == null || curMindMap.getUid() != SessionHolder.getUser()){
//                log.error("[editMindMap] [导图:"+mindMap.getTitle()+"]修改失败");
//                return Result.failed(ResultCode.FAILED);
//            }
//            //修改导图信息
//            curMindMap.setAll(mindMap);
//            curMindMap.setUpdateTime(new Date());
//            //获得一个处理者
//            ConsistencyHandler handler = ConsistencyUtil.getHandler();
//            //写入MySQL的数据（数据，写入方法，写入的策略（直接写入））
//            ConsistencyData mysqlWrite = ConsistencyUtil.getMysqlWrite(curMindMap, UpdateMysql.class, DirectScheme.class);
//            //写入Redis的数据（数据，写入方法，写入的策略（MQ间接写入））
//            ConsistencyData redisWrite = ConsistencyUtil.getRedisWrite(curMindMap, UpdateRedis.class, DirectScheme.class);
//            //写入ES的数据（数据，写入方法，写入的策略（MQ间接写入））
//            ConsistencyData esWrite = ConsistencyUtil.getEsWrite(curMindMap, UpdateEs.class, DirectScheme.class);
//            //把任务添加给处理者
//            handler.addConsistencyData(mysqlWrite);
//            handler.addConsistencyData(redisWrite);
//            handler.addConsistencyData(esWrite);
//            //处理者处理
//            handler.executeConsistency();
//
//            //打印失败
//            List<ConsistencyData> failList = handler.getFailList();
//            for (ConsistencyData consistencyData : failList) {
//                System.out.println(consistencyData.getType().getMessage());
//            }
//            return Result.success();
//        }

        MindMapDTO curMindMap = null;
        //Redisson的写锁
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock(redisProperties.lock_mindmap_read_write+mindMap.getId());
        RLock writeLock = readWriteLock.writeLock();
        RReadWriteLock readWriteLock2 = redissonClient.getReadWriteLock(redisProperties.lock_mindmap_list_read_write+SessionHolder.getUser());
        RLock writeLock2 = readWriteLock2.writeLock();
        try {
            writeLock.lock();
            writeLock2.lock();
            //导图为空，或uid对不上当前登录用户
            curMindMap = mindMapCache.getMindMap(mindMap.getId());
            if( curMindMap == null || curMindMap.getUid() != SessionHolder.getUser()){
                log.error("[editMindMap] [导图:"+mindMap.getTitle()+"]修改失败");
                return Result.failed(ResultCode.FAILED);
            }

            //修改导图信息
            curMindMap.setAll(mindMap);
            curMindMap.setUpdateTime(new Date());
            //如果修改失败
            if(!saveOrUpdate(curMindMap)){
                log.error("[editMindMap] [导图:"+mindMap.getTitle()+"]修改失败");
                return Result.failed(ResultCode.FAILED);
            }

            //缓存
            mindMapCache.addOrUpdateMindMap2(curMindMap);
        } finally {
            writeLock.unlock();
            writeLock2.unlock();
        }

        //ES
        try {
            esService.updateES(curMindMap);
        } catch (IOException e) {
            log.error("修改ES失败");
            e.printStackTrace();
        }
        //成功
        return Result.success();
    }



    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 获取导图，根据id
     * @Describe: TODO
     **/
    @SneakyThrows
    public Result getMindMapById(Long mid) {
        //参数检验
        if(DataUtil.isNaturalNumInvalid(mid)){
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        MindMapDTO mindMap = null;
        //redisson的读锁
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock(redisProperties.lock_mindmap_read_write+mid);
        RLock readLock = readWriteLock.readLock();
        try {
            readLock.lock();

            mindMap = HandleNode //流水线处理者们
                    .start(mid) //启动流水线，处理mindMap（产品）
                    .addHandler(LoadMindMap.class) // 处理者：加载mindMap
                    .addHandler(LoadRecord.class)  // 处理者：加载记录
                    .addHandler(ViewIncrement.class)     // 处理者：访问量+1
                    .finish(MindMapDTO.class); // 流水线结束，获得处理后的mindMap（产品）
        } finally {
            readLock.unlock();
        }

        return Result.success(mindMap);
    }


    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 删除导图
     * @Describe: TODO
     * @modifier: [public] [实例方法]
     **/
    public Result deleteMindMap(Long mid){
        //参数检验
        if(DataUtil.isNaturalNumInvalid(mid)){
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        //Redisson的写锁
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock(redisProperties.lock_mindmap_read_write+mid);
        RLock writeLock = readWriteLock.writeLock();
        try {
            writeLock.lock();
            //获取导图,检验用户id
            MindMap mindMap = getById(mid);
            if(mindMap == null || mindMap.getUid() != SessionHolder.getUser()){
                return Result.failed(ResultCode.FAILED);
            }
            //删除导图
            if(!removeById(mid)){
                return Result.failed(ResultCode.FAILED);
            }
            //清缓存
            mindMapCache.removeMindMap(mid);
        } finally {
            writeLock.unlock();
        }

        //删除导图相关图片
        for(String url : mindMapMapper.selectAllImage(mid)){
            deleteImage(url);
        }
        //删除数据库中保存的图片路径
        mindMapMapper.deleteImage(mid);
        //ES
        try {
            esService.deleteES(mid);
        } catch (IOException e) {
            log.error("删除ES失败");
            e.printStackTrace();
        }
        return Result.success();
    }


    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 获取公开的所有导图
     * @Describe: TODO
     * @modifier: [public] [实例方法]
     **/
    public Result getPublicMindMap(Integer page){
        //1.参数检验
        if(DataUtil.isNaturalNumInvalid(page)){
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        //2.获取导图列表
        int start = (page-1)* mmProperties.page_capacity;
        int end = start + mmProperties.page_capacity-1;

        String zsetKey = redisProperties.cache_mindmap_index +"Zset";
//        List<MindMap> curList = mindMapMapper.selectPublicAll(start,end);
//        List<MindMapListDTO> mindMapList = BeanUtil.copyToList(curList, MindMapListDTO.class);
//        loadUserInfo(mindMapList);

        List<MindMapListDTO> mindMapList = mindMapCache.getListByZset(zsetKey,start,end,null);

        if(mindMapList == null){
            log.error("[getPublicMindMap] [用户："+ SessionHolder.getUser()+"]获取所有公开导图失败");
            return Result.failed(ResultCode.FAILED);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("mindMapList",mindMapList);
        //获取页码总数
        map.put("pageCount", mindMapCache.getPageCount(zsetKey));
        return Result.success(map);
    }

//    /* ---------------------------------------------------------------------------------------------------------------------
//     * @Method  : 加载每个导图的用户信息
//     * @Describe: TODO
//     **/
//    private List<MindMapListDTO> loadUserInfo(List<MindMapListDTO> mindMapListDTO){
//        //加载每个导图列表的用户信息
//        for(MindMapListDTO mindMapDTO : mindMapListDTO){
//            UserDTO userDTO = cacheClient.queryPassThrough(
//                    redisProperties.cache_user_pre+mindMapDTO.getUid()
//                    , mindMapDTO.getUid(),UserDTO.class
//                    ,redisProperties.session_user_save_time,TimeUnit.MINUTES
//                    ,this::getUser);
//            if(userDTO == null){
//                mindMapDTO.setAvatar(mmProperties.default_mindmap_avatar);
//            }else {
//                mindMapDTO.setAvatar(userDTO.getAvatarUrl());
//            }
//        }
//        return mindMapListDTO;
//    }
//
//
//    /* ---------------------------------------------------------------------------------------------------------------------
//     * @Method  : 获取用户信息
//     * @Describe: TODO
//     **/
//    private UserDTO getUser(Long uid){
//        UserDTO userDTO = null;
//        try {
//            Result result = userClient.userInfo(uid);
//            if(ResultCode.SUCCESS.getCode() != result.getCode()){
//                log.warn("获取用户信息失败");
//                return null;
//            }
//            userDTO = (UserDTO) result.getData();
//        } catch (Exception e) {
//            log.warn("获取用户信息失败");
//            return null;
//        }
//        return userDTO;
//    }




    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 获取此用户的所有公开导图
     * @Describe: TODO
     * @modifier: [public] [实例方法]
     **/
    public Result getUserPublicMindMap(Long uid,Integer page){
        //1.参数检验
        if(DataUtil.isNaturalNumInvalid(page)
                || DataUtil.isNaturalNumInvalid(uid)){
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        //redisson的读锁
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock(redisProperties.lock_mindmap_list_read_write+SessionHolder.getUser());
        RLock readLock = readWriteLock.readLock();

        Map<String,Object> map = null;
        try {
            readLock.lock();
            int start = (page-1)* mmProperties.page_capacity;
            int end = start + mmProperties.page_capacity-1;

            //2.获取导图列表
            String zsetKey = redisProperties.cache_mindmap_list +"Zset:public:"+SessionHolder.getUser();
            List<MindMapListDTO> mindMapList = mindMapCache.getListByZset(zsetKey,start,end
                    ,redisProperties.cache_mindmap_list_time);


            if(mindMapList == null){
                log.error("[getAllMindMap] [用户："+ SessionHolder.getUser()+"]获取所有导图失败");
                return Result.failed(ResultCode.FAILED);
            }

            map = new HashMap<>();
            map.put("mindMapList",mindMapList);
            //获取页码总数
            map.put("pageCount", mindMapCache.getPageCount(zsetKey));
        } finally {
            readLock.unlock();
        }

        return Result.success(map);
    }

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 获取此用户的所有导图
     * @Describe: TODO
     * @modifier: [public] [实例方法]
     **/
    public Result getUserMindMap(Integer page){
        //1.参数检验
        if(DataUtil.isNaturalNumInvalid(page)){
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        //redisson的读锁
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock(redisProperties.lock_mindmap_list_read_write+SessionHolder.getUser());
        RLock readLock = readWriteLock.readLock();
        Map<String,Object> map = null;
        try {
            readLock.lock();
            int start = (page-1)* mmProperties.page_capacity;
            int end = start + mmProperties.page_capacity-1;
            //2.获取导图列表
            String zsetKey = redisProperties.cache_mindmap_list +"Zset:"+SessionHolder.getUser();
            List<MindMapListDTO> mindMapList = mindMapCache.getListByZset(zsetKey,start,end
                    ,redisProperties.cache_mindmap_list_time);

            if(mindMapList == null){
                log.error("[getAllMindMap] [用户："+ SessionHolder.getUser()+"]获取所有导图失败");
                return Result.failed(ResultCode.FAILED);
            }

            map = new HashMap<>();
            map.put("mindMapList",mindMapList);
            //获取页码总数
            map.put("pageCount", mindMapCache.getPageCount(zsetKey));
        } finally {
            readLock.unlock();
        }
        return Result.success(map);
    }

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 检查此用户的导图
     * @Describe: TODO
     * @modifier: [public] [实例方法]
     **/
    public Result checkMindMapByTitle(Long mid){
        //1.参数检验
        if(DataUtil.isNaturalNumInvalid(mid)){
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        //redisson的读锁
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock(redisProperties.lock_mindmap_read_write+mid);
        RLock readLock = readWriteLock.readLock();
        MindMapDTO mindMap;
        try {
            readLock.lock();
            //1.获取方案列表
            String key = redisProperties.cache_mindmap+mid+":";
            mindMap = mindMapCache.getMindMap(mid);
            if(mindMap == null){
                log.error("[getMindMap] [方案"+mid+"]不存在");
                return Result.failed(ResultCode.FAILED);
            }
        } finally {
            readLock.unlock();
        }

        return Result.success(mindMap.getId());
    }


    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 置顶导图
     * @Describe: TODO
     * @modifier: [public] [实例方法]
     **/
    public Result setTop(Long mid){
        //参数检验
        if(DataUtil.isNaturalNumInvalid(mid)){
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        //Redisson的写锁
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock(redisProperties.lock_mindmap_read_write+SessionHolder.getUser());
        RLock writeLock = readWriteLock.writeLock();
        try {
            writeLock.lock();
            //检查导图是否存在和用户ID是否一致
            MindMap mindMap = mindMapCache.getMindMap(mid);
            if(mindMap == null || SessionHolder.getUser()!=mindMap.getUid()){
                log.error("[setTop] [导图:"+mid+"]不存在");
                return Result.failed(ResultCode.FAILED);
            }

            //0^1=1; 1^1=0。使用位运算来实现1和0之间的互置
            mindMap.setIsTop((byte) (mindMap.getIsTop() ^ 1));
            mindMap.setUpdateTime(new Date());

            if(!saveOrUpdate(mindMap)){
                log.error("[setTop] 置顶[导图:"+mid+"]失败");
                return Result.failed(ResultCode.FAILED);
            }
            //缓存
            mindMapCache.addOrUpdateMindMap2(mindMap);
        } finally {
            writeLock.unlock();
        }
        return Result.success();
    }

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 发送图片
     * @Describe: TODO
     * @modifier: [public] [实例方法]
     **/
    public Result sendImage(Long mid, MultipartFile image){
        //参数检验
        if(image.isEmpty() || DataUtil.isNaturalNumInvalid(mid)){
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        MindMap mindMap = mindMapCache.getMindMap(mid);

        if(mindMap == null){
            return Result.failed(ResultCode.FAILED);
        }

        //判断图片是否超过总数
        int count = mindMapMapper.selectCountImage(mid);
        if(count >= mmProperties.image_capacity){
            return Result.failed(ResultCode.FAILED);
        }

        //获取文件后置
        String imageName = image.getOriginalFilename();
        String[] strs = imageName.split("\\.");
        String affix = strs[strs.length-1];

        //创建图片存储目录
        File mkdir = new File(fileProperties.save_file_url+"/image/mindMap/"+ SessionHolder.getUser());
        if(!mkdir.exists()){
            mkdir.mkdir();
        }

        //获取图片地址
        String filePath = "/image/mindMap/"+ SessionHolder.getUser()+"/"+ UUIDUtil.UUID_36()+"."+affix;
        //保存图片路径
        if(mindMapMapper.insertImage(mid,filePath)<= 0){
            return Result.failed(ResultCode.FAILED);
        }
        //保存图片
        BufferedImage cur = null;
        File file = new File(fileProperties.save_file_url+filePath);
        try(OutputStream outputStream = new FileOutputStream(file)){
            byte[] bytes = image.getBytes();
            outputStream.write(bytes);
            outputStream.flush();
            file.createNewFile();
            cur = ImageIO.read(file);
        } catch (IOException e) {
            log.error("[sendImage] 保存[图片:{}]出现了异常",fileProperties.save_file_url+filePath);
            e.printStackTrace();
        }

        //处理图片大小
        int width = cur.getWidth();
        int height = cur.getHeight();
        for(double i=width/440; width>440; i+=0.25){
            if(width > 440){
                if(width/i < 440){
                    width = (int)(width/i);
                    height = (int)(height/i);
                }
            }

        }

        return Result.success(filePath+"="+width+"x"+height);
    }

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 发送头图片
     * @Describe: TODO
     **/
     public Result sendRootImage(Long mid, MultipartFile image) {
         //参数检验
         if(image.isEmpty() || DataUtil.isNaturalNumInvalid(mid)){
             return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
         }


         MindMap mindMap = getById(mid);
         if(mindMap == null){
             return Result.failed(ResultCode.FAILED);
         }

         //创建图片存储目录
         File mkdir = new File(fileProperties.save_file_url+"/image/mindMap/"+ SessionHolder.getUser());
         if(!mkdir.exists()){
             mkdir.mkdir();
         }

         //获取图片地址

         //删除之前图片
         if(!mindMap.getImage().equals("./image/blog.jpeg")){
             File oldFile = new File(fileProperties.save_file_url+"/image/mindMap/"+ SessionHolder.getUser()+mindMap.getImage());
             if(oldFile.exists()){
                 oldFile.delete();
             }
         }

         //获取文件后置
         String imageName = image.getOriginalFilename();
         String[] strs = imageName.split("\\.");
         String affix = strs[strs.length-1];

         String filePath = "/image/mindMap/"+ SessionHolder.getUser()+"/"+ UUIDUtil.UUID_36()+"."+affix;

         //保存图片
         BufferedImage cur = null;
         File file = new File(fileProperties.save_file_url+filePath);
         try(OutputStream outputStream = new FileOutputStream(file)){
             byte[] bytes = image.getBytes();
             outputStream.write(bytes);
             outputStream.flush();
             file.createNewFile();
             cur = ImageIO.read(file);
         } catch (IOException e) {
             log.error("[sendImage] 保存[图片:{}]出现了异常",fileProperties.save_file_url+filePath);
             e.printStackTrace();
         }

         //Redisson的写锁
         RReadWriteLock readWriteLock = redissonClient.getReadWriteLock(redisProperties.lock_mindmap_read_write+mindMap.getId());
         RLock writeLock = readWriteLock.writeLock();

         try {
             writeLock.lock();
             mindMap.setImage(filePath);
             mindMap.setUpdateTime(new Date());
             if(!saveOrUpdate(mindMap)){
                 return Result.failed(ResultCode.FAILED);
             }

             //缓存
             mindMapCache.addOrUpdateMindMap2(mindMap);
         } finally {
             writeLock.unlock();
         }
         return Result.success();
    }


    /* ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 初始化导图
     * @Describe: TODO
     * @modifier: [private] [实例方法]
     **/
    private boolean initialize(MindMap mindMap){
        if(mindMap.getTitle() == null ){
            mindMap.setTitle(mmProperties.default_mindmap_title);
        }
        if(mindMap.getImage() == null ){
            mindMap.setImage(mmProperties.default_mindmap_image);
        }
        if(mindMap.getLike()== null){
            mindMap.setLike(0);
        }
        if(mindMap.getView()== null){
            mindMap.setView(0);
        }
        if(mindMap.getCollect()== null){
            mindMap.setCollect(0);
        }

        if(mindMap.getNodeList() == null){
            mindMap.setNodeList(mmProperties.default_mindmap_content);
        }
        if(mindMap.getIsTop() == null){
            mindMap.setIsTop((byte) 0);
        }

        if(mindMap.getCreateTime() == null){
            mindMap.setCreateTime(new Date());
        }
        if(mindMap.getUpdateTime() == null){
            mindMap.setUpdateTime(new Date());
        }
        return true;
    }

    /* ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 删除图片,根据url
     * @Describe: TODO
     * @modifier: [private] [实例方法]
    **/
    private boolean deleteImage(String url){
        File file = new File(fileProperties.save_file_url+url);
        //4.保存图片
        if(file.exists()){
            System.out.println(file.toURI());
            return file.delete();
        }
        return false;
    }

    public Result adds() {
        for(int i=1; i<=10000; i++){
            MindMap mindMap = new MindMap();
            initialize(mindMap);
            mindMap.setUid(4L);
            mindMap.setTitle("导图"+i);
            saveOrUpdate(mindMap);
        }
        return Result.success();
    }
}
